<?php

require_once 'connect.php';

function percent($num, $total) {
    return round(((float) ($num * 100) / (float) $total), 2) . "%";
}

$total = 1;
$kalonzo = 0;
$karua = 0;
$ruto = 0;
$raila = 0;
$undecided = 0;
$query = "SELECT count(*) as total FROM polls_survey where candidate is not null";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $total = $row['total'];
}
$query = "SELECT count(*) as total FROM polls_survey where candidate='kalonzo'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $kalonzo = $row['total'];
}
$query = "SELECT count(*) as total FROM polls_survey where candidate='karua'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $karua = $row['total'];
}
$query = "SELECT count(*) as total FROM polls_survey where candidate='ruto'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $ruto = $row['total'];
}
$query = "SELECT count(*) as total FROM polls_survey where candidate='raila'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $raila = $row['total'];
}
$undecided = $total - $kalonzo - $karua - $ruto - $raila;
echo "{form:{
                title:'Poll results',
                form_id:'rw',
                items:[
                        {label:
                            {text:'Kalonzo " . percent($kalonzo, $total) . ",Karua " . percent($karua, $total) . ", 
Ruto " . percent($ruto, $total) . ",Raila " . percent($raila, $total) . ",  
Undecided " . percent($undecided, $total) . "'}
                        }
                    ]
                }
          }";
?>
